//
//  ZHhttpOcTools.m
//  ZHWeibo
//
//  Created by user on 15/12/6.
//  Copyright © 2015年 heima. All rights reserved.
//

#import "ZHhttpOcTools.h"

//声明协议
@protocol ZHRequestProtocol <NSObject>
//声明协议方法-----这是AFN的私有方法,APPMAIN虽然有这个方法名,但是运行时找不到该方法,所以使用协议,声明该方法,可以使得APPMAIN可以向该类发送该消息,因为协议中的这个方法是公开的,而不是private修饰的
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                         success:(void (^)(NSURLSessionDataTask *, id))success
                                         failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;
@end
//遵守协议
@interface ZHhttpOcTools()<ZHRequestProtocol>
@end

@implementation ZHhttpOcTools

+ (instancetype)shareInstance{
    static ZHhttpOcTools *tool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[ZHhttpOcTools alloc] init];
    });
    
    return tool;
}

+ (void)requestWithMethod:(ZHRequestMethod)method andWithurlString:(NSString *)urlString andWithParams:(id)param andWithBlock:(callBackBlock)callBack{
    
    //判断方法名
    NSString *methodName = (method == GET)? @"GET" : @"POST";
    
    [[self shareInstance] dataTaskWithHTTPMethod:methodName URLString:urlString parameters:param success:^(NSURLSessionDataTask *dataTask, id response) {
        callBack(response,nil);
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        callBack(nil, error);
    }];
}


@end





















